package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.FeedbackInfo;
import com.jyy.prefabricated.service.FeedbackInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 客户反馈
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class FeedbackInfoController {

    @Autowired
    private FeedbackInfoService feedbackInfoService;

    /**
     * 新增客户反馈
     * @param feedbackInfo
     * @return
     */
    @PostMapping("/feedback-info")
    public AjaxResult insert(@Valid @RequestBody FeedbackInfo feedbackInfo){
        log.info("Rest request to insert feedback-info:{}",feedbackInfo);
        int id = feedbackInfoService.insert(feedbackInfo);
        FeedbackInfo result = feedbackInfoService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 修改客户反馈
     * @param feedbackInfo
     * @return
     */
    @PutMapping("/feedback-info")
    public AjaxResult update(@Valid @RequestBody FeedbackInfo feedbackInfo){
        log.info("Rest request to update feedback-info:{}",feedbackInfo);
        feedbackInfoService.update(feedbackInfo);
        FeedbackInfo result = feedbackInfoService.selectById(feedbackInfo.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询客户反馈详情
     * @param id
     * @return
     */
    @GetMapping("/feedback-info/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find feedback-info by id:{}",id);
        FeedbackInfo result = feedbackInfoService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除客户反馈
     * @param id
     * @return
     */
    @DeleteMapping("/feedback-info/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete feedback-info by id:{}",id);
        int row = feedbackInfoService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询客户反馈
     * @param tel 联系电话
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/feedback-info")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String tel, @RequestParam(required = false, defaultValue = "") String startDate, @RequestParam(required = false, defaultValue = "") String endDate, Pageable pageable){
        log.info("Rest request to find feedback-info by params tel:{},startDate:{},endDate:{}", tel, startDate, endDate);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<FeedbackInfo> feedbackInfo = feedbackInfoService.selectByParams(tel, startDate, endDate);
        PageInfo<FeedbackInfo> result = new PageInfo<>(feedbackInfo);
        return AjaxResult.success(result);
    }

    /**
     * 查询客户反馈
     * @param tel 联系电话
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return
     */
    @GetMapping("/feedback-info-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String tel,@RequestParam(required = false, defaultValue = "") String startDate,@RequestParam(required = false, defaultValue = "") String endDate){
        log.info("Rest request to find feedback-info by params tel:{},startDate:{},endDate:{}", tel, startDate, endDate);
        List<FeedbackInfo> result = feedbackInfoService.selectByParams(tel, startDate, endDate);
        return AjaxResult.success(result);
    }

}
